package com.shoy.myapp.ui;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import com.shoy.myapp.R;
import com.shoy.myapp.app.AppManager;
import com.shoy.myapp.base.BaseActivity;
import com.shoy.myapp.common.Utils;

/**
 * 登录页
 *
 * @author shoy
 * @created 2015/4/1
 */
public class Login extends BaseActivity {
    private long touchTime = 0;
    private static final long waitTime = 2000;

    @Override
    public int bindLayout() {
        return R.layout.login;
    }

    @Override
    public void initView(View view) {
        Button btn = (Button) findViewById(R.id.btn);
        final Context context = this;
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Utils.showToast("登录", context);
                login();
            }
        });
    }

    @Override
    public void doBusiness(Context mContext) {
//        UpdateManager.getInstance().checkAppUpdate(this, false);
    }

    private void login() {
        getOperation().forward(Main.class);
//        SortedMap<String, String> map = new TreeMap<>();
//        map.put(Constants.API_METHOD, Constants.METHOD_LOGIN);
//        map.put(Constants.PARAM_ACCOUNT, "634330628@qq.com");
//        map.put(Constants.PARAM_PASSWORD, "123456");
//        ApiTask task = new ApiTask(this) {
//            @Override
//            protected void onPostExecute(JSONObject result) {
//                super.onPostExecute(result);
//            }
//        };
//        task.setHttpMethod(Constants.HTTP_POST);
//        task.execute(map);
    }

    /**
     * 监听[返回]键事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 返回键
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            long currentTime = System.currentTimeMillis();
            if ((currentTime - touchTime) >= waitTime) {
                Utils.showToast("再按一次，退出程序", this);
                touchTime = currentTime;
            } else {
                AppManager.getInstance().exit(this);
            }
            return true;
        }
        return false;
    }
}
